<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript" src="./js/jquery.cookie.js"></script>
</head>
<body>
	<div align="center">
		<h2>欢迎来到Main页面。</h2>
		<button onclick="AllFun()">查询全部员工</button><button onclick="AddFun()">添加一个员工</button>
		<table border="1px" id="table1">
			<tr>
				<td>员工编号</td>
				<td>员工姓名</td>
				<td>员工职位</td>
				<td>员工上司编号</td>
				<td>员工入职日期</td>
				<td>员工薪资</td>
				<td>员工加薪</td>
				<td>员工部门编号</td>
			</tr>
		</table>
		
	</div>
	<script type="text/javascript">
		$(function(){
			
			AllFun();
			
			//使用sessionStorage来获取页面与页面之间的传输
			//var user_id = sessionStorage.getItem("user_id");
			//console.log(user_id);
			
			var user_id = $.cookie('user_id');
			console.log(user_id);
			
		})
	</script>
	<script type="text/javascript">
		function AllFun(){
			$.ajax({
				url:"empAllSelect",
				type:"post",
				async:true,
				dataType:"json",
				success:function(result){
					console.log(result);
					if(result.Code == 1){
						
						var first = $("#table1 tr:first-child").html();
						first = "<tr>"+first+"</tr>"
						
						var json = result;
						
						console.log(json);
						
						var str = first;
						
						for(var i = 0;i < json.data.length;i++){
							
							str += "<tr>"+
									"<td>"+json.data[i].empno+"</td>"+
									"<td>"+json.data[i].ename+"</td>"+
									"<td>"+json.data[i].job+"</td>"+
									"<td>"+json.data[i].mgr+"</td>"+
									"<td>"+json.data[i].hiredate+"</td>"+
									"<td>"+json.data[i].sal+"</td>"+
									"<td>"+json.data[i].COMM+"</td>"+
									"<td>"+json.data[i].deptno+"</td>"+
									"<td>"+"<button onclick=update1("+ json.data[i].empno +")>编辑</button>"+"</td>"+
									"<td>"+"<button onclick=delete1("+ json.data[i].empno +")>删除</button>"+"</td>"+
									"</tr>";
						}
						
						$("#table1").html(str);
						
					}else{
						alert(result.Code);
					}
				},
				error:function(error){
					console.log(error);
				}
			})
		}
	</script>
	<script type="text/javascript">
	
		//删除
		function delete1(empno){
			var empno = empno;
			
			var json = {
					"empno":empno
			}
			
			var str = JSON.stringify(json);
			
			$.ajax({
				url:"empOnedelete",
				type:"post",
				data:str,
				async:true,
				dataType:"json",
				success:function(result){
					alert(result.comment);
					AllFun();
				},
				error:function(error){
					console.error(error);
				}
			})
		}
		
		//添加
		function AddFun(){
			window.location.href = "http://localhost:8080/DyTransaction/add.html";
		}
		
		//修改
		function update1(empno){
			sessionStorage.setItem("empno",empno);
			window.location.href = "http://localhost:8080/DyTransaction/update.html";
		}
	</script>
</body>
</html>